//
// Created by 39442 on 2025/10/4.
//

#ifndef THREAD_POOL_ANY_H
#define THREAD_POOL_ANY_H
#include<memory>

class Base {
public:
    virtual ~Base() = default;
};

template <typename T>
class Derive final :public Base {
public:
    explicit Derive(T data) :data_(data){ };
    T getData() const { return data_; }
private:
    T data_;	//保存了任意类型的数据
};

class Any {
public:

    Any() = default;
    ~Any() = default;
    Any(const Any&) = delete;
    Any& operator=(Any&) = delete;
    Any(Any&&) = default;
    Any& operator=(Any&&) = default;

    //这个构造函数可以让Any类型接受任意的其他数据
    template<typename T>
    Any(T data) : base_(std::make_unique<Derive<T>>(data)) {}

    //把Any对象里存储的data数据提取出来
    template<typename T>
    T cast_()
    {
        //提取base_找到它所指向的Derive对象，从它里面去出data成员变量
        //基类指针转为派生类指针
        auto pd = dynamic_cast<Derive<T>*>(base_.get());
        if (pd == nullptr)
        {
            throw std::bad_cast();
        }
        return pd->getData();
    }

private:
    std::unique_ptr<Base> base_;
};


#endif //THREAD_POOL_ANY_H